package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

public class FirstFilterHandler implements ResourceFilterHandler{

    private ResourceFilterHandler nextHanlder;

    public void setNextHanlder(ResourceFilterHandler nextHanlder) {
        this.nextHanlder = nextHanlder;
    }

    @Override
    public void filter(List<Resource> listResource, TreeItemVo root) {

        List<Resource> firstTree = listResource.stream().filter(r -> {
            if (r.getParentResourceNo().equals(root.getId())) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());

        List<TreeItemVo> firstTreeList = firstTree.stream().map(r -> {
            TreeItemVo vo = new TreeItemVo();
            vo.setId(r.getResourceNo());
            vo.setLabel(r.getResourceName());
            return vo;
        }).collect(Collectors.toList());

        //构建关系
        root.setChildren(firstTreeList);

        if(null != nextHanlder){
            nextHanlder.filter(listResource, root);
        }
    }
}
